﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Extensions;
using Microsoft.Extensions.Logging;
using Wfxin.Models;
using Wfxin.Core;
using Wfxin.Web.Api.MQ; 
using SystemFramework.Models;
using Wfxin.SystemFramework;

namespace Wfxin.Web.Api.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    [Route("api/[controller]/[action]")]
    //[ApiController]
    public class HomeController : ControllerBase
    {
        static readonly ILogger _Logger = LogManager.Create<HomeController>();
        private readonly ILogger _logger = null;
        private readonly IServiceProvider _serviceProvider;


        private readonly IOrderPublishMessageSender _PublishMessageSender = null;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceProvider"></param>
        /// <param name="logger"></param>
        /// <param name="publishMessageSender"></param>
        public HomeController(IServiceProvider serviceProvider,ILogger<HomeController> logger,
               IOrderPublishMessageSender publishMessageSender)
        {
            _serviceProvider = serviceProvider;
            _logger = logger;

            _PublishMessageSender = publishMessageSender;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Index()
        {

            _Logger.LogInformation("{0} Information {1}", "测试100", "测试200");
            _Logger.LogWarning("{0} Warning {1}", "测试100", "测试200");
            _Logger.LogError("{0} Error {1}", "测试100", "测试200");
            _Logger.LogCritical("{0} Critical {1}", "测试100", "测试200");

            _Logger.LogTrace("{0} Trace {1}", "测试100", "测试200");
            _Logger.LogDebug("{0} Debug {1}", "测试100", "测试200");
             
            _logger.WriteLog(LogLevel.Warning, "{controller} Hello {action}", "Home", "Index");

            var ReqData = new RequestData<RedisOptions>()
            {
                RequestContentLength = Request.ContentLength.HasValue ? Request.ContentLength.Value : 0L,
                RequestContentType = Request.ContentType,
                RequestHost = Request.GetUserRealIp(),
                RequestMethod = Request.Method,
                RequestPath = Request.Path.HasValue ? Request.Path.Value : string.Empty,
                RequestScheme = Request.Scheme,
                UserName = "home"
            };
            _PublishMessageSender.SendAsync(ReqData);


            return new JsonResult(00);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostForm()
        {
            ReqInfo reqInfo = Request.Model<ReqInfo>();
              
            _Logger.LogInformation(reqInfo.ToJson());
            _Logger.LogInformation("{0} Information {1}", "测试100", "测试200");
            _Logger.LogWarning("{0} Warning {1}", "测试100", "测试200");
            _Logger.LogError("{0} Error {1}", "测试100", "测试200");
            _Logger.LogCritical("{0} Critical {1}", "测试100", "测试200");



            return new JsonResult(00);
        }


        //    public float Index(Product product, int count,
        //Type taxStrategyServiceType)
        //    {
        //        using (var scope = _serviceProvider.CreateScope())
        //        {
        //            var taxStrategy = (ITaxStrategy)scope.ServiceProvider
        //              .GetRequiredService(taxStrategyServiceType);
        //            var price = product.Price * count;
        //            return price + taxStrategy.CalculateTax(price);
        //        }
        //    }
    }
}
